﻿package com.pool 
{
	import flash.events.Event;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import org.dm.net.NetMgr;
	import org.dm.res.ResMgr;
	import org.dm.events.ResEvent;
	import org.dm.display.DisplayMgr;
	
	import com.pool.core.*;
	import com.pool.events.*;
	
	/**
	 * ...
	 * @author Scott
	 */
	public class PoolWorld
	{
		private var resMgr:ResMgr;
		private var netMgr:NetMgr;
		
		private var _container:DisplayObjectContainer;
		private var _login:Login;
		private var _game:PoolGame;
		private var _rooms:Rooms;
		private var _desks:Desks;
		
		public function PoolWorld():void 
		{
			_container = DisplayMgr.getDisplay("stage");
		}
		
		public function init():void 
		{
			ResMgr.instance.addRes(["win.swf","write.swf"]);
			ResMgr.instance.start();
			ResMgr.instance.addEventListener(ResEvent.COMPLETE, onLoadedRes);
		}
		
		private function onLoadedRes(e:ResEvent):void 
		{
			_login = new Login();
			_rooms = new Rooms();
			_desks = new Desks();
			_game = new PoolGame();
			
			DisplayMgr.add("login.main", _login);
			DisplayMgr.add("rooms.main", _rooms);
			DisplayMgr.add("desks.main", _desks);
			DisplayMgr.add("game.main", _game);
			
			_login.addEventListener(LoginEvent.LOGIN, onLogin);
			_rooms.addEventListener(RoomEvent.LOGINROOM, onLoginRoom);
			_desks.addEventListener(DeskEvent.LOINGAME, onLoginDesk);
			
			_container.addChild(_login.display);
		}
		
		private function onLogin(e:LoginEvent):void 
		{
			_login.checkUser();
			_login.addEventListener(LoginEvent.LOGINOK, onLoginOK);
			_login.addEventListener(LoginEvent.LOGINERR, onLoginErr);
		}
		
		private function onLoginOK(e:LoginEvent):void 
		{
			_login.getUserData();
			_login.removeEventListener(LoginEvent.LOGINOK, onLoginOK);
			_login.removeEventListener(LoginEvent.LOGINERR, onLoginErr);
			_login.addEventListener(LoginEvent.GETDATAOK,onGetUserDataOK);
		}
		
		private function onGetUserDataOK(e:LoginEvent):void
		{
			_login.removeEventListener(LoginEvent.GETDATAOK, onGetUserDataOK);
			
			_container.removeChild(_login.display);
			_login.stopAll();
			_container.addChild(_rooms.display);
			_rooms.init();
		}
		
		private function onGetUserDataErr(e:LoginEvent):void
		{
			
		}
		
		private function onLoginErr(e:LoginEvent):void 
		{
			trace("wef");
		}
		
		private function onLoginRoom(e:RoomEvent):void 
		{
			if (1)
			{
				_container.removeChild(_rooms.display);
				//_rooms.stopAll();
				_container.addChild(_desks.display);
				_desks.init();
			}	
		}
		
		private function onLoginDesk(e:DeskEvent):void 
		{
			if (1)
			{
				
				_container.removeChild(_desks.display);
				_container.addChild(_game.display);
				//_game.start();
			}
		}
	}
}